"""
Roo Code配置文件
用于连接DeepSpeed MOE + Qwen3-Coder API系统
"""

import json
from typing import Dict, Any

class RooCodeConfig:
    """Roo Code配置管理"""
    
    def __init__(self):
        self.api_url = "http://localhost:8000"
        self.api_key = "roo-code-key-2024"
        self.model_name = "qwen3-coder-480b-a35b-instruct"
    
    def generate_roo_code_config(self) -> Dict[str, Any]:
        """生成Roo Code配置文件"""
        return {
            "apiProvider": {
                "type": "openai",
                "id": "deepspeed-qwen3-coder",
                "name": "DeepSpeed MOE + Qwen3-Coder",
                "baseUrl": self.api_url,
                "apiKey": self.api_key,
                "models": [
                    {
                        "id": self.model_name,
                        "name": "Qwen3-Coder 480B (MOE)",
                        "description": "基于DeepSpeed MOE优化的Qwen3-Coder模型，适合代码生成和调试",
                        "contextLength": 262144,
                        "inputPrice": 0.0,
                        "outputPrice": 0.0,
                        "supportsFunctionCalling": True,
                        "supportsVision": False,
                        "maxTokens": 4096
                    }
                ],
                "supportsStreaming": True,
                "customHeaders": {
                    "Content-Type": "application/json",
                    "User-Agent": "RooCode/DeepSpeed-MOE-Client"
                }
            },
            "customCommands": [
                {
                    "name": "生成代码",
                    "prompt": "请根据用户需求生成相应的代码"
                },
                {
                    "name": "调试代码",
                    "prompt": "请分析并调试用户提供的代码，找出错误并提供修复方案"
                },
                {
                    "name": "代码审查",
                    "prompt": "请对提供的代码进行全面审查，包括最佳实践、安全性、性能等方面"
                },
                {
                    "name": "优化代码",
                    "prompt": "请优化提供的代码，提高性能和可读性"
                }
            ],
            "slashCommands": [
                {
                    "name": "/explain",
                    "description": "解释代码功能",
                    "prompt": "请详细解释以下代码的功能和工作原理："
                },
                {
                    "name": "/refactor", 
                    "description": "重构代码",
                    "prompt": "请重构以下代码，使其更加清晰和高效："
                },
                {
                    "name": "/test",
                    "description": "生成测试用例",
                    "prompt": "请为以下代码生成全面的测试用例："
                }
            ]
        }
    
    def save_config(self, filename: str = "roo-code-config.json"):
        """保存配置文件"""
        config = self.generate_roo_code_config()
        
        with open(filename, 'w', encoding='utf-8') as f:
            json.dump(config, f, indent=2, ensure_ascii=False)
        
        print(f"配置已保存到: {filename}")
        return config

def create_claude_code_config():
    """创建Claude Code配置"""
    return {
        "anthropic": {
            "apiKey": "claude-code-key-2024",
            "baseUrl": "http://localhost:8000",
            "models": [
                {
                    "id": "qwen3-coder-480b-a35b-instruct",
                    "name": "Qwen3-Coder 480B (MOE)",
                    "description": "基于DeepSpeed MOE优化的Qwen3-Coder模型",
                    "contextLength": 262144,
                    "inputPrice": 0.0,
                    "outputPrice": 0.0,
                    "supportsFunctionCalling": True,
                    "supportsVision": False
                }
            ]
        }
    }

def create_continue_dev_config():
    """创建Continue.dev配置"""
    return {
        "models": [
            {
                "title": "Qwen3-Coder (DeepSpeed MOE)",
                "provider": "openai",
                "model": "qwen3-coder-480b-a35b-instruct",
                "apiKey": "local-dev-key-2024",
                "apiBase": "http://localhost:8000",
                "contextLength": 262144,
                "completionOptions": {
                    "temperature": 0.1,
                    "topP": 0.95,
                    "maxTokens": 4096
                }
            }
        ],
        "tabAutocompleteModel": {
            "title": "Qwen3-Coder Completion",
            "provider": "openai",
            "model": "qwen3-coder-480b-a35b-instruct",
            "apiKey": "local-dev-key-2024", 
            "apiBase": "http://localhost:8000"
        },
        "embeddingsProvider": {
            "provider": "transformers.js"
        }
    }

def create_vscode_settings():
    """创建VSCode设置"""
    return {
        "continue.apiKey": "local-dev-key-2024",
        "continue.apiBase": "http://localhost:8000",
        "continue.useOpenAi": True,
        "continue.defaultModel": "qwen3-coder-480b-a35b-instruct",
        "continue.tabAutocompleteModel": {
            "title": "Qwen3-Coder Autocomplete",
            "provider": "openai",
            "model": "qwen3-coder-480b-a35b-instruct",
            "apiKey": "local-dev-key-2024",
            "apiBase": "http://localhost:8000"
        }
    }

if __name__ == "__main__":
    # 生成所有配置
    config_generator = RooCodeConfig()
    
    # 保存Roo Code配置
    config_generator.save_config("roo-code-config.json")
    
    # 保存其他工具的配置
    with open("claude-code-config.json", "w", encoding="utf-8") as f:
        json.dump(create_claude_code_config(), f, indent=2, ensure_ascii=False)
    
    with open("continue-dev-config.json", "w", encoding="utf-8") as f:
        json.dump(create_continue_dev_config(), f, indent=2, ensure_ascii=False)
    
    with open("vscode-settings.json", "w", encoding="utf-8") as f:
        json.dump(create_vscode_settings(), f, indent=2, ensure_ascii=False)
    
    print("所有配置文件已生成完成！")